package com.clouds.wms.inv;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.bean.Invma;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmjService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.service.InvmaService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;
import com.clouds.wms.util.SysParams;

@Controller
@RequestMapping("/invma")
public class InvmaController {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="invmaService")
	private InvmaService invmaService;
//	@Resource(name="invmbService")
//	private InvmbService InvmbService;
	@Resource(name="cmsmjService")
	private CmsmjService cmsmjService;
	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	@RequestMapping(value = "/invmaMain.xhtml")
	public String invmaMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		model.addAttribute("allCmsmjList", cmsmjService.getCmsmjListByMjac(SysParams.DOCUMENT_WORK));
		return "inv/invma";
	}
	@RequestMapping(value = "/invmaList.xhtml")
	public String invmaList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String maaa = RequestHelper.getString(request, "maaa");
		String maab = RequestHelper.getString(request, "maab");
		String maac = RequestHelper.getString(request, "maac");
		String startMaad = RequestHelper.getString(request, "startMaad");
		String endMaad = RequestHelper.getString(request, "endMaad");
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = invmaService.getInvmaList(maaa, maab, maac, startMaad, endMaad, pageNumber, pageSize);
		List<Invma> list = (List<Invma>) pb.getData();
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 添加采购单单头
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/addInvma.xhtml", method = RequestMethod.POST)
	public String addInvma(HttpServletRequest request, HttpServletResponse response, Model model, Invma invma) throws SysException, IOException {
		String maaa = invma.getMaaa();
		String maab = invma.getMaab();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		Invma invmatmp = invmaService.getInvma(maaa,maab);
		if(invmatmp != null && invmatmp.getMaaa() != null && !"".equals(invmatmp.getMaaa())) {
			out.print("该采购订单的编号已经存在，请重新录入!");
			return null;
		}
		invma.setMaad(Function.getDateString(new Date()));
		invmaService.saveInvma(invma);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + maab + "]的工单!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	
}
